package com.booklove.controller.school;


import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.School;
import com.booklove.domain.query.SchoolQuery;
import com.booklove.result.Result;
import com.booklove.service.school.SchoolService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController("schoolCLL")
@RequestMapping("/schools")
@RequiredArgsConstructor// 使用基本注解的注入形式，仅仅注入需要的变量
public class SchoolController {

    private final SchoolService schoolService;

    /**
     * 保存新增
     */
    @PostMapping("/")
    Result<String> saveSchool(@RequestBody School schoolFormDTO) {
        School school = new School();
        BeanUtils.copyProperties(schoolFormDTO, school);
        schoolService.save(school);
        return Result.success();
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/id")
    Result<String> deleteSchool(@RequestParam("id") Long id) {
        schoolService.removeById(id);
        return Result.success();

    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    Result<School> getSchool(@PathVariable Long id) {

        return Result.success(schoolService.getById(id));

    }


    /**
     * 编辑数据
     *
     * @param schoolFormDTO 实体
     * @return 编辑结果
     */
    @PutMapping("/")
    Result<String> updateSchool(@RequestBody School schoolFormDTO) {
        schoolService.updateById(schoolFormDTO);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param schoolQuery 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    PageDTO<School> schoolsPageQuery(SchoolQuery schoolQuery) {
        return schoolService.querySchoolsPage(schoolQuery);
    }

}

